package com.shire.web.console.service.user;

import com.shire.web.console.dao.user.SystemUserDao;
import com.shire.web.console.model.user.SystemUser;
import com.shire.web.console.model.user.SystemUserExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SystemUserService {
    @Resource
    private SystemUserDao userMapper;

    public List<SystemUser> getKfUsers(Integer opId){
        SystemUserExample userExample = new SystemUserExample();
        userExample.createCriteria().andIsKfEqualTo(1).andOpUserIdEqualTo(opId);
        return userMapper.selectByExample(userExample);
    }

    public SystemUser checkLogin(String userName, String password){
        SystemUserExample userExample = new SystemUserExample();
        userExample.createCriteria().andUserNameEqualTo(userName).andPasswordEqualTo(password).andIsKfNotEqualTo(1).andStatusEqualTo(1);
        List<SystemUser> userList = userMapper.selectByExample(userExample);
        if (userList.size() < 1)
            return null;
        else return userList.get(0);
    }

    public int saveUser(SystemUser user){
        if (user.getId() == null){
            return userMapper.insertSelective(user);
        }else {
            return userMapper.updateByPrimaryKeySelective(user);
        }
    }

    public SystemUser selectById(Integer id){
        return userMapper.selectByPrimaryKey(id);
    }
}
